package web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import pojo.Brand;
import service.brandService.brandService;
import service.brandServiceImpl.brandServiceImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/addServlet")
public class addServlet extends HttpServlet {
    private brandService brandservice=new brandServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        BufferedReader br = req.getReader();//获取axios中携带的数据（请求体）
        String s = br.readLine();
        System.out.println("这是JSON数据"+s);
        Brand brand = JSON.parseObject(s, Brand.class);//将json数据转化为brand对象
        brandservice.add(brand);//调用add方法添加对象
        resp.getWriter().write("yes");//提交成功后返回yes
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
